#!/bin/sh
#
# webvirtmgr-console  WebVirtMgr Console noVNC Proxy
#
# chkconfig:   - 98 02
# description: WebVirtMgr Console noVCN Proxy Server

### BEGIN INIT INFO
# Provides:
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $syslog
# Default-Stop: 0 1 6
# Short-Description: WebVirtMgr Console noVNC Proxy
# Description: WebVirtMgr Console noVNC Proxy Server
### END INIT INFO


# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status



prog="webvirtmgr-console"
exec="/srv/www/htdocs/webvirtmgr/console/$prog"
lockfile="/var/lock/subsys/$prog"
pidfile="/var/run/webvirtmgr/$prog.pid"
logfile="/var/log/$prog.log"
username="nginx"


test -x $exec || exit 5
PIDFILE=$pidfile

if [ ! -d /var/run/webvirtmgr ]; then
        mkdir /var/run/webvirtmgr
        chown $username /var/run/webvirtmgr
fi


# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

start() {

    echo -n $"Starting $prog: "
        ## Start daemon with startproc(8). If this fails
        ## the echo return value is set appropriate.

        # NOTE: startproc return 0, even if service is
        # already running to match LSB spec.
        startproc -p $PIDFILE $exec

     #daemon --user $username --pidfile $pidfile "$exec &>/dev/null & echo \$! > $pidfile"
     # Remember status and be verbose
        rc_status -v

}

stop() {
    echo -n $"Stopping $prog: "
    killproc -TERM -p $PIDFILE   $exec

}


case "$1" in
    start)
         # Remember status and be verbose
        start
        rc_status -v
        ;;
    stop)
         # Remember status and be verbose
        stop
        rc_status -v
        ;;
    restart)
        $0 stop
        sleep 5
        $0 start
         # Remember status and be verbose
        rc_status -v
        ;;
    reload)
        $0 restart
         rc_status -v
        ;;
    force-reload)
        $0 restart
         rc_status -v
        ;;
    status)
         rc_status -v
        ;;
    condrestart|try-restart)
        $0 restart
        rc_status -v
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?
